package com.demo.datastructure.binarySearch;

/**
 * @description: LETCODE 33  搜索旋转排序数组
 * @author: ljr
 * @time: 2021/5/14 13:38
 */
public class BinarySearch11 {

    public static int bsearch(int[] nums, int target) {
        int n = nums.length;
        if(n == 1){
           return nums[0] == target? 0:-1;
        }
        int low = 0;
        int high = n-1;
        while (low <= high){
            int mid = low + (high-low >> 1);
            if(nums[mid] == target){
                return mid;
            }

            if(nums[0] <= nums[mid]){
                if(target < nums[mid] && target >= nums[low]){
                    high = mid-1;
                }else {
                    low = mid+1;
                }
            }else{
                if(target > nums[mid] && target <= nums[high]){
                    low = mid+1;
                }else{
                    high = mid-1;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
       int[] nums = new int[]{1,3,5};
       //int[] nums = new int[]{4,5,6,7,0,1,2};
       int index = BinarySearch11.bsearch(nums,1);
       System.out.println(index);
    }
}
